
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>odespy.runtests &mdash; Odespy API 0.2 documentation</title>
    
    <link rel="stylesheet" href="../../_static/pyramid.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="top" title="Odespy API 0.2 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Neuton&amp;subset=latin" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Nobile:regular,italic,bold,bolditalic&amp;subset=latin" type="text/css" media="screen" charset="utf-8" />
<!--[if lte IE 6]>
<link rel="stylesheet" href="../../_static/ie6.css" type="text/css" media="screen" charset="utf-8" />
<![endif]-->

  </head>
  <body>

    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../../np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">Odespy API 0.2 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for odespy.runtests</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">odespy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">problems</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">scitools.std</span> <span class="kn">import</span> <span class="n">plot</span><span class="p">,</span> <span class="n">hold</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">joblib</span>  <span class="c"># need joblib for storing database nested dict</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&#39;This script requires joblib to be installed.&#39;</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

<span class="n">newresults</span> <span class="o">=</span> <span class="p">{}</span>

<div class="viewcode-block" id="test_Linear"><a class="viewcode-back" href="../../runtests.html#odespy.runtests.test_Linear">[docs]</a><span class="k">def</span> <span class="nf">test_Linear</span><span class="p">():</span>
    <span class="c"># Note: MidpointIter needs max_iter=10 and eps_iter=1E-6;</span>
    <span class="c"># lower tolerances do not give errors to machine precision.</span>
    <span class="c"># No problem with Linear2t so it is the nonlinearity that</span>
    <span class="c"># fools MidpointIter.</span>

    <span class="c"># Problems 2&gt;a&gt;0: RKF45 gives wrong results, error 0.67</span>
    <span class="c"># RungeKutta3 and BogackiShampine error approx 1E-8</span>

    <span class="c"># Problems a&lt;0: numerous, also for Linear2t which is linear</span>

    <span class="n">kwargs</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mf">1.2</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
    <span class="n">problems</span> <span class="o">=</span> <span class="n">Linear1</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">),</span> <span class="n">Linear2</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">),</span> <span class="n">Linear2t</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="n">r</span> <span class="o">=</span> <span class="n">tester</span><span class="p">(</span><span class="n">problems</span><span class="p">,</span> <span class="n">list_available_solvers</span><span class="p">(),</span> <span class="n">compare_tol</span><span class="o">=</span><span class="mf">1E-14</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">r</span>
</div>
<div class="viewcode-block" id="test_disk"><a class="viewcode-back" href="../../runtests.html#odespy.runtests.test_disk">[docs]</a><span class="k">def</span> <span class="nf">test_disk</span><span class="p">():</span>
    <span class="n">problem</span> <span class="o">=</span> <span class="n">Linear2</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mf">1.2</span><span class="p">,</span><span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span><span class="n">c</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
    <span class="n">solver</span> <span class="o">=</span> <span class="n">ForwardEuler</span><span class="p">(</span><span class="n">problem</span><span class="o">.</span><span class="n">f</span><span class="p">,</span> <span class="n">disk_storage</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
    <span class="n">solver</span><span class="o">.</span><span class="n">set_initial_condition</span><span class="p">(</span><span class="n">problem</span><span class="o">.</span><span class="n">U0</span><span class="p">)</span>
    <span class="n">tp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">1000000</span><span class="p">)</span>
    <span class="c"># tp = problem.default_parameters()[&#39;time_points&#39;]</span>
    <span class="n">u</span><span class="p">,</span> <span class="n">t</span> <span class="o">=</span> <span class="n">solver</span><span class="o">.</span><span class="n">solve</span><span class="p">(</span><span class="n">tp</span><span class="p">)</span>
    <span class="k">print</span> <span class="s">&#39;made u&#39;</span><span class="p">,</span> <span class="n">u</span><span class="o">.</span><span class="n">size</span>
    <span class="n">u0</span> <span class="o">=</span> <span class="n">u</span><span class="p">[:,</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">print</span> <span class="s">&#39;made u0&#39;</span>
    <span class="k">if</span> <span class="n">tp</span><span class="o">.</span><span class="n">size</span> <span class="o">&gt;</span> <span class="mi">500000</span><span class="p">:</span>
        <span class="n">u</span> <span class="o">=</span> <span class="n">u</span><span class="p">[</span><span class="o">-</span><span class="mi">100</span><span class="p">:]</span>
        <span class="n">t</span> <span class="o">=</span> <span class="n">t</span><span class="p">[</span><span class="o">-</span><span class="mi">100</span><span class="p">:]</span>
        <span class="n">error</span> <span class="o">=</span> <span class="n">problem</span><span class="o">.</span><span class="n">verify</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span>
        <span class="k">print</span> <span class="n">error</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">error</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="k">print</span> <span class="s">&#39;u memmap:&#39;</span><span class="p">,</span> <span class="n">u</span><span class="p">[:</span><span class="mi">10</span><span class="p">]</span>
    <span class="n">u</span><span class="p">[:]</span> <span class="o">=</span> <span class="mf">5.5</span>
    <span class="n">u</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
    <span class="k">print</span> <span class="n">u</span><span class="p">[:</span><span class="mi">10</span><span class="p">]</span>
    <span class="k">return</span> <span class="p">{</span><span class="s">&#39;Linear2&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s">&#39;t&#39;</span><span class="p">:</span> <span class="n">t</span><span class="p">,</span> <span class="s">&#39;ForwardEuler&#39;</span><span class="p">:</span> <span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">error</span><span class="p">)}}</span>
</div>
<div class="viewcode-block" id="test_Exponential"><a class="viewcode-back" href="../../runtests.html#odespy.runtests.test_Exponential">[docs]</a><span class="k">def</span> <span class="nf">test_Exponential</span><span class="p">():</span>
    <span class="n">problem</span> <span class="o">=</span> <span class="n">Exponential</span><span class="p">(</span><span class="n">a</span><span class="o">=-</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">A</span><span class="o">=</span><span class="mf">1.5</span><span class="p">)</span>
    <span class="c"># test f_args, jac_kwargs etc</span>
    <span class="n">methods</span> <span class="o">=</span> <span class="p">[</span><span class="n">Vode</span><span class="p">]</span>
    <span class="k">for</span> <span class="n">method</span> <span class="ow">in</span> <span class="n">methods</span><span class="p">:</span>
        <span class="n">solver</span> <span class="o">=</span> <span class="n">method</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="test_VanDerPol"><a class="viewcode-back" href="../../runtests.html#odespy.runtests.test_VanDerPol">[docs]</a><span class="k">def</span> <span class="nf">test_VanDerPol</span><span class="p">(</span><span class="n">mu</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="n">problem</span> <span class="o">=</span> <span class="n">VanDerPolOscillator</span><span class="p">(</span><span class="n">mu</span><span class="o">=</span><span class="n">mu</span><span class="p">,</span> <span class="n">U0</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">])</span>
    <span class="n">d</span> <span class="o">=</span> <span class="n">problem</span><span class="o">.</span><span class="n">default_parameters</span><span class="p">()</span>
    <span class="n">tp</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="s">&#39;time_points&#39;</span><span class="p">]</span>
    <span class="c"># test f_args, jac_kwargs etc</span>
    <span class="n">methods</span> <span class="o">=</span> <span class="p">[</span><span class="n">Vode</span><span class="p">,</span> <span class="n">RK4</span><span class="p">,</span> <span class="n">RungeKutta4</span><span class="p">,</span> <span class="n">ForwardEuler</span><span class="p">,</span> <span class="n">BackwardEuler</span><span class="p">]</span>
    <span class="k">for</span> <span class="n">method</span> <span class="ow">in</span> <span class="n">methods</span><span class="p">:</span>
        <span class="n">name</span> <span class="o">=</span> <span class="n">method</span><span class="o">.</span><span class="n">__name__</span>
        <span class="k">print</span> <span class="n">name</span>
        <span class="n">solver</span> <span class="o">=</span> <span class="n">method</span><span class="p">(</span><span class="n">problem</span><span class="o">.</span><span class="n">f</span><span class="p">,</span> <span class="n">jac</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">jac</span><span class="p">,</span>
                        <span class="n">atol</span><span class="o">=</span><span class="n">d</span><span class="p">[</span><span class="s">&#39;atol&#39;</span><span class="p">],</span> <span class="n">rtol</span><span class="o">=</span><span class="n">d</span><span class="p">[</span><span class="s">&#39;rtol&#39;</span><span class="p">])</span>
        <span class="n">solver</span><span class="o">.</span><span class="n">set_initial_condition</span><span class="p">(</span><span class="n">problem</span><span class="o">.</span><span class="n">U0</span><span class="p">)</span>
        <span class="n">u</span><span class="p">,</span> <span class="n">t</span> <span class="o">=</span> <span class="n">solver</span><span class="o">.</span><span class="n">solve</span><span class="p">(</span><span class="n">tp</span><span class="p">)</span>
        <span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">u</span><span class="p">[:,</span><span class="mi">0</span><span class="p">],</span> <span class="n">legend</span><span class="o">=</span><span class="n">name</span><span class="p">,</span>
             <span class="n">legend_fancybox</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">legend_loc</span><span class="o">=</span><span class="s">&#39;upper left&#39;</span><span class="p">)</span>
        <span class="n">hold</span><span class="p">(</span><span class="s">&#39;on&#39;</span><span class="p">)</span>
        <span class="n">e</span> <span class="o">=</span> <span class="n">problem</span><span class="o">.</span><span class="n">verify</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">e</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="k">print</span> <span class="n">e</span>

</div>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">resfile</span> <span class="o">=</span> <span class="s">&#39;test_problems_odespy.dat&#39;</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">resfile</span><span class="p">):</span>
        <span class="n">database</span> <span class="o">=</span> <span class="n">joblib</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">resfile</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">database</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="c"># Run tests</span>
    <span class="c">#test_VanDerPol(float(sys.argv[1]))</span>
    <span class="c">#r = test_Linear()</span>
    <span class="n">r</span> <span class="o">=</span> <span class="n">test_disk</span><span class="p">()</span>
    <span class="kn">import</span> <span class="nn">pprint</span>
    <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;tmp.2&#39;</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span>
    <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">pprint</span><span class="o">.</span><span class="n">pformat</span><span class="p">(</span><span class="n">r</span><span class="p">))</span>
    <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="c"># We come here only if all tests pass</span>
    <span class="n">joblib</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">database</span><span class="p">,</span> <span class="n">resfile</span><span class="p">)</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../../np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">Odespy API 0.2 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Liwei Wang and Hans Petter Langtangen.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
    </div>
  </body>
</html>